package com.auoem.radio.common.utils;

import android.app.Activity;
import android.content.Context;
import android.widget.Toast;

import static android.widget.Toast.makeText;

public class ToastUtils {

    private static final String TAG = "ToastUtils";
    private static Toast mToast;

    public static void showShortToast(Context ctx, int strResId) {
        showToast(ctx, strResId, Toast.LENGTH_LONG);
    }

    public static void showShortToast(Context ctx, String msg) {
        showToast(ctx, msg, Toast.LENGTH_SHORT);
    }

    public static void showLongToast(Context ctx, int strResId) {
        showToast(ctx, strResId, Toast.LENGTH_LONG);
    }

    public static void showLongToast(Context ctx, String msg) {
        showToast(ctx, msg, Toast.LENGTH_LONG);
    }

    public static void showToast(final Context ctx, final String msg, final int duration) {
        if (null == ctx || ((Activity) ctx).isFinishing()) return;
        ((Activity) ctx).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Thread.currentThread().setName("Au_Toast");
//                if (mToast == null) {
//                    mToast = makeText(ctx, msg, duration);
//                } else {
//                    mToast.setText(msg);
//                    mToast.setDuration(duration);
//                }
//                mToast.show();
                showToast(ctx, msg);
            }
        });
//		Toast.makeText(ctx, msg, duration).show();
    }

    public static void showToast(Context ctx, int resId, int duration) {
//        if (null == ctx) return;
//        if (mToast == null) {
//            mToast = makeText(ctx, resId, duration);
//            mToast.show();
//            LogM.d(TAG, "showToast new");
//        } else {
//            mToast.setText(resId);
//            mToast.setDuration(duration);
//            mToast.show();
//            LogM.d(TAG, "showToast again");
//        }

        if (null == ctx || ((Activity) ctx).isFinishing()) return;
        ((Activity) ctx).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Thread.currentThread().setName("Au_Toast");
//                if (mToast == null) {
//                    mToast = makeText(ctx, msg, duration);
//                } else {
//                    mToast.setText(msg);
//                    mToast.setDuration(duration);
//                }
//                mToast.show();
                showToast(ctx, resId);
            }
        });

    }


    /**
     * 之前显示的内容
     */
    private static String oldMsg;
    /**
     * Toast对象
     */
    private static Toast toast = null;
    /**
     * 第一次时间
     */
    private static long oneTime = 0;
    /**
     * 第二次时间
     */
    private static long twoTime = 0;

    /**
     * 显示Toast
     *
     * @param context
     * @param message
     */
    public static void showToast(Context context, String message) {
        if (toast == null) {
            toast = makeText(context, message, Toast.LENGTH_SHORT);
            toast.show();
            oneTime = System.currentTimeMillis();
        } else {
            twoTime = System.currentTimeMillis();
            if (message.equals(oldMsg)) {
                if (twoTime - oneTime > Toast.LENGTH_SHORT) {
                    toast.show();
                }
            } else {
                oldMsg = message;
                toast.setText(message);
                toast.show();
            }
        }
        oneTime = twoTime;
    }

    /**
     * 显示Toast
     *
     * @param context
     * @param message
     */
    public static void showToast(Context context, int message) {
        showToast(context, context.getResources().getString(message));
    }

}
